//
//  MHRepoViewModel.m
//  MiaoCircle
//
//  Created by 许正荣 on 2018/5/12.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHRepoViewModel.h"
#import "MHIllegalListModel.h"

@interface MHRepoViewModel()

@property(nonatomic, strong) NSArray *illegeList;
@end

@implementation MHRepoViewModel

- (instancetype)initWithServices:(id<MHViewModelServices>)services params:(NSDictionary *)params{
    if (self = [super initWithServices:services params:params]) {
        //        MHCircleInfo *info =  [MHCircleInfo new];
        self.info = params[MHViewModelUtilKey];
        
    }
    return self;
}


-(void)initialize
{
    [super initialize];
    self.title = @"举报";
    self.rightTitle = @"确定";
    YYCache *cache=[YYCache cacheWithName:MHCacheGlobleKey];
    self.illegeList = [cache objectForKey:MHIllegalList];
//    self.illegeList = nil;
    if(self.illegeList.count <= 0)
    {
        
        void (^doNext)(NSArray *) = ^(NSArray *illegalList){
            self.illegeList = illegalList;
        };
        [[[[[MHHTTPService sharedInstance] getIllegalList] mh_parsedResults] doNext:doNext] subscribeNext:^(id x) {
            
        }];
    
    }
    @weakify(self);
    RAC(self,dataSource) = [RACObserve(self, illegeList) map:^(NSArray * blacks) {
        @strongify(self)
        MHCommonGroupViewModel *group1 = [MHCommonGroupViewModel groupViewModel];
        group1.itemViewModels = [self dataSourceWithcircleMembers:blacks];
        return @[group1];
    }];
    
    self.repoCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
       @strongify(self);
        return  [[self.services.client reportRecentStoryWithRecentId:self.info.autoId reportTxt:input] doNext:^(id x) {
            [MBProgressHUD mh_showTips:@"举报成功"];
            [self.services popViewModelAnimated:YES];
        }];
    }];
}

- (NSArray *)dataSourceWithcircleMembers:(NSArray *)blackList {
    if (MHObjectIsNil(blackList) || blackList.count == 0) return nil;
    NSMutableArray *viewModels = [[blackList.rac_sequence map:^(MHIllegalListModel *illegal) {
        MHCommonItemViewModel *viewModel = [MHCommonItemViewModel itemViewModelWithTitle:illegal.txt icon:@"circle_radio_normal"];
        viewModel.selectIcon = @"circle_radio_sel";
        return viewModel;
    }].array mutableCopy];
    
    return viewModels ?: @[] ;
}

@end
